;; Copyright (C) 2011, 2012, 2013, 2014 Markus Kollmar
;;
;; This file is part of ESCAD.
;;
;; ESCAD is free software: you can redistribute it and/or modify
;; it under the terms of the GNU Affero General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; ESCAD is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU Affero General Public License for more details.
;;
;; You should have received a copy of the GNU Affero General Public License
;; along with ESCAD.  If not, see <http://www.gnu.org/licenses/>.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(in-package "COMMON-LISP-USER")
(defpackage :de.markus-herbert-kollmar.escad.export
  (:use :common-lisp :escad)
  (:nicknames :escad.export)
  (:export :export2dot :export2svg)
  (:shadow #:cos)
  (:documentation "Export current view to dot format (graphviz) and svg (XML/HTML)."))

(in-package :de.markus-herbert-kollmar.escad.export)


;; USER CONFIG START
(defparameter *dot-header* "//Generated by escad expansion export.")
(defparameter *svg-header* "//Generated by escad expansion export.")
;; USER CONFIG END


(defun export2dot (expansion-symbol-name &optional (filename "view.dot"))
  "expansion-symbol-name [relative-file-name] -> filename
Export view to a textual dot (graphviz) file with name <view.dot>, which is viewable by a text editor.
Do this if symbol will be activated!"
  (let ((absolute-filename (concatenate 'string *escad-view-dir* filename)))
    (with-open-file (out absolute-filename :direction :output :if-exists :supersede)
		    (with-standard-io-syntax
		     (princ *dot-header* out) (write-char #\newline out)
		     (princ "digraph schematic {" out) (write-char #\newline out)
		     (dolist (name (ls :exclude-taxonomy '("escad.symbol._escad" "escad.symbol._thisView")))
		       (princ (concatenate 'string name ";") out)
		       (write-char #\newline out))
		     (dolist (name (lr))
		       (with-slots (comment ref_from ref_to taxonomy) (gethash name escad::*relations*)
				   (loop for i from 0 to (- (length ref_from) 1) do
					 (princ (concatenate 'string (string (nth i ref_from))
							     " -> "
							     (string (nth i ref_to))
							     ";") out)
					 (write-char #\newline out))))
		     (write-char #\newline out)
		     (princ "}" out)))
    absolute-filename))

(defun export2svg (expansion-symbol-name &optional (filename "view.svg"))
  "expansion-symbol-name [relative-file-name] -> filename
Export view to a svg (xml) graphical file with name <view.svg>, which is viewable e.g. by the browser firefox."
  (let ((absolut-output-filename (concatenate 'string *escad-view-dir* filename)))
    (export2dot expansion-symbol-name *escad_tmp_file*)
    #+clisp (sys::shell (concatenate 'string "dot -Tsvg -o " absolut-output-filename " " *escad_tmp_file*))
    #-clisp '("Sorry, function not available. Please type 'dot -Tsvg -o outputfile.svg inputfile.dot' in your shell manualy." '(1 "GPL3"))
    absolut-output-filename))
